package com.lucky.contract.util;

import org.apache.poi.hssf.usermodel.*;

import java.io.OutputStream;

public class CreateExcel {
    public void createExcelToDisk(String[] headers, String tablename, String[][] rows,
                                  OutputStream os) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(tablename);
        HSSFRow row = sheet.createRow(0);
        // style可以用来设置表格样式
        HSSFCellStyle style = wb.createCellStyle();
        HSSFCell cell =null;
        for (int j = 0; j < headers.length; j++) {
            cell = row.createCell(j);
            String value = headers[j];
            cell.setCellValue(value);
        }
        for (int k = 0; k < rows.length; k++) {
            row = sheet.createRow(k + 1);
            for (int f = 0; f < rows[k].length; f++) {
                row.createCell(f).setCellValue(rows[k][f]);
            }
        }
        try {
            wb.write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

